package com.cafe.action.admin;

import java.util.Iterator;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.cafe.model.ProductCategory;
import com.cafe.service.ProductCategoryService;
import com.malamute.core.action.BaseAction;
import com.malamute.core.action.CrudMethod;
import com.malamute.core.action.PrepareHelper;

/**
 * @author Aceguan
 * @description 菜谱类型
 * @date 2012-2-25 下午12:59:58
 * @version v1.0
 */
public class ProductCategoryAction extends BaseAction implements CrudMethod,
		PrepareHelper {
	private ProductCategory productCategory;
	@Autowired
	private ProductCategoryService productCategoryService;

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#save()
	 */
	@Override
	public String save() throws Exception {
		productCategoryService.saveProductCategory(productCategory);
		return ACTION;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#update()
	 */
	@Override
	public String update() throws Exception {
		productCategoryService.updateProductCategory(productCategory);
		return ACTION;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#list()
	 */
	@Override
	public String list() throws Exception {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(ProductCategory.class);
		for (Iterator it = searchParam.keySet().iterator(); it.hasNext();) {
			String key = (String) it.next();
			String[] values = (String[]) searchParam.get(key);
			criteria.add(Restrictions.like(key, "%" + values[0] + "%"));
		}
		// TODO :这里又可以做修改了。。跳转页面按照默认规则
		// setListResult("customer_main");
		// page.setCurrentPageNum(1);
		productCategoryService.listProductCategoriesByPage(criteria, page);
		return LIST;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#search()
	 */
	@Override
	public String search() throws Exception {
		return this.list();
	}

	public String form() throws Exception {
		if (productCategory != null) {
			setProductCategory(productCategoryService
					.getProductCategoryById(productCategory.getId()));
		}
		return FORM;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#prepareInput()
	 */
	@Override
	public void prepareInput() throws Exception {

	}

	public ProductCategory getProductCategory() {
		return productCategory;
	}

	public void setProductCategory(ProductCategory productCategory) {
		this.productCategory = productCategory;
	}

}
